<template>
	<div class="system-dept-container">
		<!-- 查询条件-->
		<el-card shadow="hover">
			<el-form inline v-model="params" size="small">
				<el-form-item>
					<el-input v-model="params.resourceId" placeholder="搜索模块ID"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.resourceName" placeholder="搜索资源名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.resourceType" placeholder="搜索资源类型(1.目录 2.菜单 3.按钮)"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.authCode" placeholder="搜索权限编码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.isGeneral" placeholder="搜索通用菜单"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.path" placeholder="搜索路径"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.component" placeholder="搜索组件"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.icon" placeholder="搜索菜单图标"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.url" placeholder="搜索外部链接地址"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.isCache" placeholder="搜索是否缓存页面"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.parentId" placeholder="搜索父级菜单ID"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.sortValue" placeholder="搜索排序，越小越靠前"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.isFrame" placeholder="搜索是否是外部链接"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="initTableData">
						<el-icon>
							<elementSearch />
						</el-icon>
						查询
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<!--分割线-->
		<el-divider></el-divider>
		<!--数据展示和分页-->
		<el-card shadow="never">
			<template #header>
				<div class="card-header">
					<span>资源表列表</span>
					<!--新增等操作区域-->
					<el-button size="small" type="success" class="ml10" @click="openAddWindow">
						<el-icon>
							<elementDocumentAdd />
						</el-icon>
						新增资源表
					</el-button>
				</div>
			</template>
			<!--表格展示区-->
			<el-table :data="tableData.data" style="width: 100%" default-expand-all>
				<el-table-column align="center" label="模块ID" prop="resourceId" show-overflow-tooltip />
				<el-table-column align="center" label="资源名称" prop="resourceName" show-overflow-tooltip />
				<el-table-column align="center" label="资源类型(1.目录 2.菜单 3.按钮)" prop="resourceType" show-overflow-tooltip />
				<el-table-column align="center" label="权限编码" prop="authCode" show-overflow-tooltip />
				<el-table-column align="center" label="通用菜单" prop="isGeneral" show-overflow-tooltip />
				<el-table-column align="center" label="路径" prop="path" show-overflow-tooltip />
				<el-table-column align="center" label="组件" prop="component" show-overflow-tooltip />
				<el-table-column align="center" label="菜单图标" prop="icon" show-overflow-tooltip />
				<el-table-column align="center" label="外部链接地址" prop="url" show-overflow-tooltip />
				<el-table-column align="center" label="是否缓存页面" prop="isCache" show-overflow-tooltip />
				<el-table-column align="center" label="父级菜单ID" prop="parentId" show-overflow-tooltip />
				<el-table-column align="center" label="排序，越小越靠前" prop="sortValue" show-overflow-tooltip />
				<el-table-column align="center" label="是否是外部链接" prop="isFrame" show-overflow-tooltip />
				<el-table-column align="center" label="操作" show-overflow-tooltip width="230">
					<template #default="scope">
						<el-button type="text" @click="openEditWindow(scope.row)">修改</el-button>
						<el-popconfirm
							confirm-button-text="删除"
							cancel-button-text="取消"
							icon-color="red"
							title="确定删除这条记录吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button type="text">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<!--分页-->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="params.pageNum"
				background
				v-model:page-size="params.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>

		<AddSysResource ref="addRef" @initTableData="initTableData" />
		<EditSysResource ref="editRef" @initTableData="initTableData" />
	</div>
</template>

<script lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import AddSysResource from '/@/views/system/resource/component/addSysResource.vue';
import EditSysResource from '/@/views/system/resource/component/editSysResource.vue';
import { delSysResource, getSysResourceList } from '/@/api/system/resource/index';

export default {
	name: 'tenantInfo',
	components: {
		AddSysResource,
		EditSysResource,
	},
	setup(props) {
		const addRef = ref();
		const editRef = ref();

		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
			},
			params: {
				pageNum: 1,
				pageSize: 10,
				resourceId: '',
				resourceName: '',
				resourceType: '',
				authCode: '',
				isGeneral: '',
				path: '',
				component: '',
				icon: '',
				url: '',
				isCache: '',
				parentId: '',
				sortValue: '',
				isFrame: '',
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			getSysResourceList(state.params).then((res) => {
				state.tableData.data = res.data;
				state.tableData.total = res.extra.total;
			});
		};

		//分页大小改变
		const onHandleSizeChange = (val: number) => {
			state.params.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.params.pageNum = val;
			initTableData();
		};

		// 打开新增菜单弹窗
		const openAddWindow = () => {
			addRef.value.openWindows();
		};

		const openEditWindow = (row: any) => {
			editRef.value.openWindows(row.resourceId);
		};

		const delRow = (row: any) => {
			delSysResource(row.resourceId).then(() => {
				initTableData();
			});
		};

		// 页面加载时
		onMounted(() => {
			initTableData();
		});

		return {
			addRef,
			editRef,
			initTableData,
			onHandleSizeChange,
			onHandleCurrentChange,
			openAddWindow,
			openEditWindow,
			delRow,
			...toRefs(state),
		};
	},
};
</script>

<style scoped></style>
